//***********************************************************************
//	File:		HeatSeeker.cpp
//	Author:		
//	Course:		SGP 1405
//	Purpose:	
//***********************************************************************
//***********************************************************************
//	Purposfully left blank for all comments regarding this file 
//	
//	
//	
//***********************************************************************



#include "HeatSeeker.h"
#include "../Message/DestroyEntityMessage.h"

#include "../GameStates/Play.h"

#include "../SGD Wrappers/SGD_GraphicsManager.h"

#include <cassert>

HeatSeeker::HeatSeeker(float posX, float posY, Entity* owner)
: SGD::Listener(this)
{
	RegisterForEvent("IM_AN_ENEMY");

	SetDamage(75); 
	//SetFinalSound(); 
	SetHasCollided(false); 
	//SetImage();
	//SetInitialSound(); 
	SetOwnerType(owner);
	SetPosition(SGD::Point{ posX, posY }); 
	SetRotation(owner->GetRotation());
	SetSize(SGD::Size{ 8, 8 }); 
	SetTurnRate(SGD::PI / 4);
	SetVelocity(SGD::Vector{ 0, -1 }); 
	SetSpeed(50); 
	SetAcceleration(20); 

}

//Interface
void HeatSeeker::Update(float dt)
{
	// Update the Emitter
	SGD::Rectangle cameraPos = Play::GetInstance()->GetCameraRect();

	if (m_ptPosition.x < cameraPos.left
		|| m_ptPosition.x + m_szSize.width > cameraPos.right
		|| m_ptPosition.y < cameraPos.top
		|| m_ptPosition.y + m_szSize.height > cameraPos.bottom)
	{
		SetOwnerType(nullptr);
		DestroyEntityMessage* entityToDestroy = new DestroyEntityMessage(this);
		entityToDestroy->QueueMessage();
		entityToDestroy = nullptr;
	}

	if (m_pTarget != nullptr)
	{
		SGD::Vector toTarget = m_pTarget->GetPosition() - GetPosition(); 

		float angle = m_vVelocity.ComputeAngle(toTarget); 
		if (m_vVelocity.ComputeSteering(toTarget) > 0)
		{
			//m_fRotation = angle * m_fRotation * dt;
			m_vVelocity.Rotate(angle * m_fTurnRate * dt); 
		}
		else
		{
			//m_fRotation = -angle * m_fRotation * dt;
			m_vVelocity.Rotate(- angle * m_fTurnRate * dt);
		}
	}

		SetSpeed(GetSpeed() * GetAcceleration() * dt);

		SGD::Vector newVelocity = { 0, -1 };
		newVelocity.Rotate(GetRotation());
		newVelocity = newVelocity * GetSpeed();
		//newVelocity.Normalize(); 

		SetVelocity(newVelocity);
	

	// Final line of function
	Entity::Update(dt); 
}

void HeatSeeker::Render()
{
	// Render the Emitter
	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance();

	SGD::Rectangle myRect = GetRect();

	SGD::Point camera = Play::GetInstance()->GetCameraPos();

	SGD::Point playerPos = GetPosition();
	myRect.left -= camera.x;
	myRect.right -= camera.x;
	myRect.top -= camera.y;
	myRect.bottom -= camera.y;

	pGraphics->DrawRectangle(myRect, { 255, 0, 0 });

}

void HeatSeeker::HandleCollision(const IEntity* pOther)
{
	switch (pOther->GetType())
	{
	case ENT_ANKLEBITER:
	{

	}
	case ENT_BASHER:
	{

	}
	case ENT_DISRUPTOR:
	{

	}
	case ENT_MECHATROOPER:
	{

	}
	case ENT_MOTHERBRAIN:
	{

	}
	case ENT_PWNER:
	{

	}
	case ENT_SPIDERMINE:
	{

	}
	case ENT_STRIKER:
	{

	}
	case ENT_TANKOSAURUS:
	{
		DestroyEntityMessage* pEntityToDestroy = new DestroyEntityMessage(this); 
		pEntityToDestroy->QueueMessage(); 
	}
	}
}

void HeatSeeker::HandleEvent(const SGD::Event* pEvent)
{
	m_pTarget = (Entity*)pEvent->GetSender(); 
}

SGD::Rectangle HeatSeeker::GetRect() const
{
	return SGD::Rectangle
	{
		GetPosition() - GetSize() / 2, 
		GetPosition() + GetSize() / 2
	};
}

void HeatSeeker::UpdateTarget()
{

}
